package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

@Repository("CrawlerfilestatusDAO")
@Transactional
public class CrawlerfilestatusDAOImpl extends AbstractJpaDao<Crawlerfilestatus> implements CrawlerfilestatusDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Crawlerfilestatus.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public CrawlerfilestatusDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Crawlerfilestatus findCrawlerfilestatusById(Integer id) throws DataAccessException {

		return findCrawlerfilestatusById(id, -1, -1);
	}

	@Transactional
	public Crawlerfilestatus findCrawlerfilestatusById(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerfilestatusById", startResult, maxRows, id);
			return (Crawlerfilestatus) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Crawlerfilestatus> findCrawlerfilestatusByName(String name) throws DataAccessException {

		return findCrawlerfilestatusByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilestatus> findCrawlerfilestatusByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfilestatusByName", startResult, maxRows, name);
		return new LinkedHashSet<Crawlerfilestatus>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerfilestatus> findCrawlerfilestatusByNameContaining(String name) throws DataAccessException {

		return findCrawlerfilestatusByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilestatus> findCrawlerfilestatusByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfilestatusByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Crawlerfilestatus>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerfilestatus> findCrawlerfilestatusByDescription(String description) throws DataAccessException {

		return findCrawlerfilestatusByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilestatus> findCrawlerfilestatusByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfilestatusByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Crawlerfilestatus>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerfilestatus> findCrawlerfilestatusByDescriptionContaining(String description)
			throws DataAccessException {

		return findCrawlerfilestatusByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilestatus> findCrawlerfilestatusByDescriptionContaining(String description, int startResult,
			int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfilestatusByDescriptionContaining", startResult, maxRows,
				description);
		return new LinkedHashSet<Crawlerfilestatus>(query.getResultList());
	}

	@Transactional
	public Crawlerfilestatus findCrawlerfilestatusByPrimaryKey(Integer id) throws DataAccessException {
		return findCrawlerfilestatusByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Crawlerfilestatus findCrawlerfilestatusByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerfilestatusByPrimaryKey", startResult, maxRows, id);
			return (Crawlerfilestatus) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Crawlerfilestatus> findCrawlerfilestatussByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerfilestatussByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Crawlerfilestatus> findAllCrawlerfilestatuss() throws DataAccessException {

		return findAllCrawlerfilestatuss(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilestatus> findAllCrawlerfilestatuss(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllCrawlerfilestatuss", startResult, maxRows);
		return new LinkedHashSet<Crawlerfilestatus>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfilestatus> findCrawlerfilestatusByCompositeProperities(String sql, int startResult,
			int maxRows) {
		return new LinkedHashSet<Crawlerfilestatus>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Crawlerfilestatus> findCrawlerfilestatussByDetachedCriteria(CmiPagination page)
			throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Crawlerfilestatus.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Crawlerfilestatus> crawlerfilestatuss = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return crawlerfilestatuss;
	}

	@Transactional
	public Crawlerfilestatus findCrawlerfilestatusByPhysicalPrimaryKey(String name) throws DataAccessException {
		return findCrawlerfilestatusByPhysicalPrimaryKey(name, -1, -1);
	}

	@Transactional
	public Crawlerfilestatus findCrawlerfilestatusByPhysicalPrimaryKey(String name, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerfilestatusByPhyscialPrimaryKey", startResult, maxRows, name);
			return (Crawlerfilestatus) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Crawlerfilestatus entity) {
		return true;
	}
}
